package com.irdstudio.efp.esb.service.impl.basicfn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.basicfn.FiveLeClaManHistChangeReqBean;
import com.irdstudio.efp.esb.service.bo.resp.basicfn.FiveLeClaManHistChangeRespBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.basicfn.FiveLeClaManHistChangeService;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("fiveLeClaManHistChangeService")
public class FiveLeClaManHistChangeServiceImpl implements FiveLeClaManHistChangeService {
    private static Logger logger = LoggerFactory.getLogger(FiveLeClaManHistChangeServiceImpl.class);

    @Override
    public FiveLeClaManHistChangeRespBean applyFiveLeClaManHistChange(FiveLeClaManHistChangeReqBean fiveLeClaManHistChangeReqBean) throws ESBException {
        logger.info("======>调用贷款还款结果查询接口【" + TraceUtil.getGlblSrlNo() + "_30230012_01】开始<======");
        FiveLeClaManHistChangeRespBean fiveLeClaManHistChangeRespBean = null;
        try {
            EsbRespServiceBean recvBean = ESBClientFactory.buildClient()
                    .withBody(fiveLeClaManHistChangeReqBean)
                    .withTradeNo(ESBConstant.TRADE_NO_30230012)
                    .withScene(ESBConstant.SECNE_NO_01)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                fiveLeClaManHistChangeRespBean = recvBean.getBody(FiveLeClaManHistChangeRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用贷款还款结果查询接口【30230012_01】结束，返回信息：" + JSONObject.toJSONString(fiveLeClaManHistChangeRespBean));
        } catch (Exception e) {
            logger.error("调用贷款还款结果查询接口【30230012_01】出现异常：" + e.getMessage());
            throw new ESBException("调用贷款还款结果查询接口【30230012_01】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用贷款还款结果查询接口【" + TraceUtil.getGlblSrlNo() + "_30230012_01】结束<======");
        }
        return fiveLeClaManHistChangeRespBean;
    }
}